package com.querie.model.beans;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class PrecisionRecallStatistics {
	private static int totalNumberOfSessions = 0;
	private static int precisionArrayCount[] = new int[11];
	private static int recallArrayCount[] = new int[11];
	
	private static BufferedWriter statWriter;
	
	public static int getTotalNumberOfSessions() {
		return totalNumberOfSessions;
	}

	public static int[] getPrecisionArrayCount() {
		return precisionArrayCount;
	}



	public static int[] getRecallArrayCount() {
		return recallArrayCount;
	}

	public static void incrementPRStatistics(double precision, double recall)
	{
		precision = precision*10;
		recall = recall * 10;
		totalNumberOfSessions++;
		precisionArrayCount[(int)precision]++;
		recallArrayCount[(int)recall]++;
	}
	
	public static void persistDataInExcel(int configurationId)
	{
		
		File statsFile = new File("resources/experiments/configuration"+configurationId+"/precisionrecallstats.csv");
		if (!statsFile.exists())
		{
			try {
				statsFile.createNewFile();
				
				statWriter = new BufferedWriter(new FileWriter(statsFile));
				statWriter.newLine();
				statWriter.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		try {
			statWriter.write("Number of total sessions for all runs counted, "+totalNumberOfSessions);
			statWriter.newLine();
			int consecutiveSum = 0;
			for (int index = precisionArrayCount.length - 1; index >= 0; index--)
			{
				consecutiveSum+=precisionArrayCount[index];
				statWriter.write("Number of precision > "+ (0.1*index)+" and  <"+0.1*(index+1)+","+precisionArrayCount[index]+","+((double)(precisionArrayCount[index] * 100)/totalNumberOfSessions)+"%"+","+consecutiveSum+","+(((double)consecutiveSum)*100)/totalNumberOfSessions+"%");
				statWriter.newLine();
			}
			
			consecutiveSum = 0;
			for (int index = recallArrayCount.length-1; index > 0; index--)
			{
				consecutiveSum+=recallArrayCount[index];
				statWriter.write("Number of recall > "+ (0.1*index)+" and  <"+0.1*(index+1)+","+recallArrayCount[index]+","+((double)(recallArrayCount[index] * 100)/totalNumberOfSessions)+"%"+","+consecutiveSum+","+(((double)consecutiveSum)*100)/totalNumberOfSessions+"%");
				statWriter.newLine();
			}
			statWriter.newLine();
			statWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static  void closeAndFlushStatWriter()
	{
		try {
			statWriter.flush();
			statWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
